﻿
var path = require( 'path' );
var async = require( 'async' );
var rootPath = __dirname;
var nodeContext = require( path.join( rootPath, "/Database/WorkFlowNodeContext" ) );
var taskContext = require( path.join( rootPath, "/Database/TaskContext" ) );

//解析节点数据
exports.handlerNode = function ( node, workflowName, parameters, returnCall ) {

    var that = this;

    if(node.parameters != ""){
        var needPara = node.parameters.split( ',' );
        
        if ( needPara.length ) {
            for ( var i in needPara ) {
                var para = needPara[i];
                if ( parameters[para] == undefined ) {
                    returnCall( "参数不全!", null );
                    return;
                }
            }
        }
    }

    if(node.NodeName == "End"){

       returnCall( null, null, node );
       return;     
    }
    
    if ( node.handlerModule && node.handlerFunction ) {
        var md = require( path.join( rootPath, node.handlerModule ) );
        if ( md ) {
            var handler = md[node.handlerFunction];
            
            if ( handler && typeof handler == "function" ) {
                
                handler( parameters, function ( err, returnVal ) {
                    
                    if(node.type == "activity"){
                        //handler of activity must return EmpId of approver
                        
                        returnCall( err, returnVal, node );
                        return;

                    }else if( node.type == "decision" ){
                        //handler of decision must return operate result
                        
                        nodeContext.GetNodeByWorkflowNameAndNodeName(workflowName,node.NodeName,returnVal,function(err, currentNode){
                            
                            nodeContext.GetNodeByWorkflowNameAndNodeName(workflowName,currentNode.transition,"",function( error, nextNode){
                                
                                that.handlerNode(nextNode,workflowName,parameters,returnCall);
                            });
                            
                        });
                    }

                });

            }
        }
    }

};

/*
 * 流程操作
 */
exports.Operate = function( nodeName, flowName, condition, businessType, parameters, successCall){

    var that = this;

    var NodeInfo = null,
        Task = null,
        isEnd = false;

    async.series(
        {
            getNextNodeInfo: function ( callback ) {

                nodeContext.GetNextNodeInfo(flowName, nodeName, condition, function (err, node) {
                    NodeInfo = node;
                    callback( err , node);
                });
                
            },

            handlerNode: function( callback ){
                
                if( NodeInfo.NodeName == "End"){
                    isEnd = true;
                    callback( null, null);    
                }

                that.handlerNode( NodeInfo, flowName, parameters, function ( err, result, returnNode ) {

                    var node = returnNode;

                    if( node.NodeName == "End"){
                        isEnd = true;
                        callback( null, null);    
                    }

                    Task = {
                        ApplicationID : parameters.ApplicationID,
                        BusinessTypeID : businessType,
                        workFlowID : node.WorkFlowId,
                        workFlowProcessId : node.Id,
                        Node : node.NodeName,
                        ApprocedEmpID : result,
                        OperatorID : 0,
                        GroupCode : ''  
                    };
                    callback( err, Task);

                });    
            },

            updateTask : function( callback ){
                if( !isEnd ){
                    
                    taskContext.UpdateTaskOperatorID( parameters.ApplicationID, parameters.OperatorID,function( err, result){
                        callback( err, result);  
                    });

                }else{

                    taskContext.ClearTaskByApplicationID( parameters.ApplicationID, function( err, result){
                        callback( err, result);  
                    });
                }
                    
            },

            insertTask : function( callback ){

                if(!isEnd){

                    taskContext.InsertTask(Task, function( err , result ){
                        callback( err, result );    
                    });   

                }else{
                    
                    callback( null, null);
                } 
            }
            
        },
        function ( err, results ) {

            if ( successCall && typeof successCall == "function" ) { successCall( err, results ); }
        });
};
